<template>
  <!-- 安全库存 -->
  <div class="content">
    <div>
      <el-button size="mini" @click="handleGetUntreated">{{ $t('btn.reload') }}</el-button>
      <el-button type="primary" size="mini" @click="handleExport">{{ $t('btn.toExcel') }}</el-button>
    </div>
    <div class="header">
      <div class="header-name">
        <search :options="selectData" :msg="tacitly" @seek="collect" @hand="handleQuerySonClass" />
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
        <filtration :ftype="ftype" :msg="tacitly" :select-data="selectData" :table-header="tableHeader1" @seachData="handleQuerySonClass" />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        serial
        sortable
      >
        <!--    物料编码    -->
        <template v-slot:btnStates="col">
          <el-link type="primary" @click="jumpMateriel(col.scope.row)">
            {{ col.scope.row.fnumber }}
          </el-link>
        </template>
        <!--最小库存-->
        <template v-slot:tagSlot="col">
          <el-input-number
            v-model="col.scope.row.fminstock"
            :min="0"
            size="mini"
          />
        </template>
        <!--最大库存-->
        <template v-slot:billSlot="col">
          <el-input-number
            v-model="col.scope.row.fmaxstock"
            :min="0"
            size="mini"
          />
        </template>
        <!--安全库存-->
        <template v-slot:orderSlot="col">
          <el-input-number
            v-model="col.scope.row.fsafestock"
            :min="0"
            size="mini"
          />
        </template>
        <!--常规物料-->
        <template v-slot:text2Slot="col">
          <el-tag v-if="col.scope.row.froutine">是</el-tag>
          <el-tag v-else type="danger">否</el-tag>
        </template>
        <!--操作-->
        <template v-slot:btnSlot="col">
          <el-button type="primary" class="btn" size="mini" @click="updateMaterial(col.scope.row)">
            {{ $t('market.renewal') }}
          </el-button>
        </template>
      </jc-table>
      <cardTab :tab-arr="tabArr" field="fname" @search="handleQueryBomList" />
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="handleGetUntreated"
      />
    </div>
  </div>
</template>

<script>
import {
  queryMaterialStockList,
  updateMaterialStockListCorrect
} from '@/api/repertory/safeStock'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import filtration from '@/components/filtration'

export default {
  name: 'SafeStock',
  components: {
    cardTab: () => import('@/components/cardTab'),
    search,
    filtration
  },
  mixins: [searData],
  data() {
    return {
      ftype: 72,
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      isCorrect: '1', // 正常、异常
      isMatervisity: false, // 物料弹窗
      indexSelf: 0, // 下标
      delArr: [],
      listData: [],
      tabArr: [
        { label: '餐台', name: '餐台' },
        { label: '餐椅', name: '餐椅' },
        { label: '包装', name: '包装' },
        { label: '木板', name: '木板' },
        { label: '管材', name: '管材' },
        { label: '包材', name: '包材' },
        { label: '泡沫', name: '泡沫' },
        { label: '海绵', name: '海绵' },
        { label: '纸箱', name: '纸箱' },
        { label: '纸平卡', name: '纸平卡' },
        { label: '布', name: '布' },
        { label: '瓷砖', name: '瓷砖' },
        { label: '下架', name: '下架' },
        { label: '中柱', name: '中柱' },
        { label: '软体', name: '软体' },
        { label: '底盘', name: '底盘' },
        { label: '面板', name: '面板' },
        { label: '台面', name: '台面' },
        { label: '螺丝', name: '螺丝' }
      ],
      // 表头
      tableData: [], // 销售数据
      tableHeight: '73vh',
      tableHeader: [
        { label: this.$t('market.fmodel'), prop: 'fmodel' },
        { label: this.$t('market.fmaterial'), type: 'states', prop: 'fnumber', minWidth: '110px' },
        { label: this.$t('shop.fname2'), prop: 'fname' },
        { label: this.$t('shop.fspecificaTion'), prop: 'fspecification', minWidth: '110px' },
        { label: this.$t('market.fdescripTion'), prop: 'fdescription', minWidth: '200px' },
        { label: this.$t('market.minStock'), type: 'tag', prop: 'fminstock' },
        { label: this.$t('market.maxStock'), type: 'bill', prop: 'fmaxstock' },
        { label: this.$t('market.safeStock'), type: 'order', prop: 'fsafestock' },
        { label: this.$t('market.froutine'), type: 'text2', prop: 'froutine' },
        { label: this.$t('market.handle'), type: 'btn', minWidth: '100px' }
      ],
      tableHeader1: []
    }
  },
  created() {
    this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 240px)' : '73vh'
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          queryMaterialStockList({
            pageSize: 100000,
            pageNum: 1,
            isCorrect: this.isCorrect,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.list
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '安全库存')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取安全库存
    async handleGetUntreated(ev) {
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        pageNum: this.currentPage,
        pageSize: this.size,
        isCorrect: this.isCorrect,
        search: this.searCollData.concat(this.details)
      }
      const RES = await queryMaterialStockList(DATA)
      this.tableData = RES.data.list
      this.total = RES.data.total
    },
    // 更新库存——正常库存
    updateMaterial(ev) {
      updateMaterialStockListCorrect(ev).then(res => {
        if (res.code === 0) {
          // this.handleGetUntreated()
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    // 成品 原材料
    handleQueryBomList(ev) {
      this.searCollData = []
      this.currentPage = 1
      this.searCollData = ev
      this.searchSon = ev
      this.handleGetUntreated()
    }
  }
}
</script>
<style lang="scss" scoped>
.content {
  @include listBom;
  .header{
    position:relative;
    .header-name{
      width: 100%;
    }
    .btn{
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
  .el-table{
    &::v-deep .el-input-number .el-input__inner{
      padding: 0;
      width: 80px;
    }
    &::v-deep .el-input-number .el-input{
      width: 80px;
    }
    &::v-deep .el-input-number--mini {
      width: 80px;
    }
    &::v-deep .el-input-number__increase{
      display: none;
    }
    &::v-deep .el-input-number__decrease{
      display: none;
    }
    &::v-deep .el-table__footer-wrapper tbody td{
      font-size: 13px;
      background: #fdf6ec!important;
    }
  }
}
</style>
